package com.example.Sao.MyActivity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.Sao.Adapter.UseDepartmentAdpter;
import com.example.Sao.Data.DepartmentList;
import com.example.Sao.MyBean.PCInfoBean;
import com.example.Sao.MyFragment.LogonDialog;
import com.example.Sao.MyFragment.RoomChooseFragment;
import com.example.Sao.ObjectBox.DeviceEntity;
import com.example.Sao.ObjectBox.DeviceEntity_;
import com.example.Sao.R;
import com.example.Sao.Serivce.SerialPortService;
import com.example.Sao.Tools.DepartmentFileutil;
import com.example.Sao.Tools.ObjectBoxHelper;
import com.example.Sao.Tools.SharedPrefsUtil;
import com.google.gson.Gson;
import com.wang.avi.AVLoadingIndicatorView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import io.reactivex.functions.Consumer;


public class MainActivity extends MyBaseActivity implements View.OnClickListener {
    //部门列表适配器
    private UseDepartmentAdpter useDepartmentAdpter;
    //控件
    private TextView num_partment,left_partment,ready_partment,all_device,iv_home;
    private ImageView list;
    private RecyclerView recyclerView;
    //异步通信组件
    private MHandler mHandler;
    //退出时间
    private long mExitTime;
    //状态值
    private final int DIALOGFRAGMNET_STUATE = 0;
    private final int BATTERY_STUATE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置状态栏颜色
//        StatusColorUtil.setColor(this, Color.parseColor("#f42d56"));
        //绑定控件
        initview();
        //初始化Handler
        mHandler = new MHandler(this);
//        initTest(ObjectBoxHelper.getInstance());
//        new LogonDialog().show(getSupportFragmentManager(),"");
//        ObjectBoxHelper.getInstance().removeall();
    }
    @Override
    protected void onResume() {
        super.onResume();
//        startService(new Intent(this, SerialPortService.class));
        //加载数据
        initdata();
        //是否第一次登陆 否则进行登陆
        if (!(SharedPrefsUtil.getValue(this,"logon","")!=null&&
                "succes".equals(SharedPrefsUtil.getValue(this,"logon","")))) {
        //登陆界面的Fragment
            LogonDialog logonDialog = new LogonDialog();
            logonDialog.setmContext(this);
            logonDialog.setLogonStaute(new LogonDialog.onLogonStaute() {
                @Override
                public void onLogonStauteBack(int staute) {
                          if (staute==1){
                              SharedPrefsUtil.putValue(MainActivity.this,"logon", "succes");
                          }
                }
            });
            logonDialog.show(getSupportFragmentManager(),"");
        }
//        init(ObjectBoxHelper.getInstance());
        //打开本地服务
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            //android 8.0以上 转为前台服务 避免被扼杀
//            startForegroundService(new Intent(this, PCService.class));
//        } else {
//            //android 8.0以下
//            startService(new Intent(this, PCService.class));
//        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
//                Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
//                i.putExtra("android.intent.extra.KEY_CONFIRM", false);
//                startActivity(i);
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 绑定控件
     */
    private void initview(){
        //绑定ID
        iv_home = findViewById(R.id.iv_home);
        all_device = findViewById(R.id.all_device);
        left_partment = findViewById(R.id.left_partment);
        ready_partment = findViewById(R.id.ready_partment);
        num_partment = findViewById(R.id.num_partment);
        list = findViewById(R.id.lsi);
        recyclerView = findViewById(R.id.RecyclerView);
        //设置点击监听回调
        iv_home.setOnClickListener(this::onClick);
        list.setOnClickListener(this::onClick);
//        //数字累加动画
//        NumAnim.startAnim(today_check,256,1000);
//        NumAnim.startAnim(ready_check,1245,1000);
//        NumAnim.startAnim(check_num,1234,1000);
    }

    /**点击监听回调
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_home:
                //桌面返回
                Intent i = new Intent(Intent.ACTION_MAIN);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.addCategory(Intent.CATEGORY_HOME);
                startActivity(i);
                break;
            case R.id.lsi:
                //弹出PopWindow
                popwindowshow(list);
                break;
            default:
                break;
        }
    }

    /**构建选择器
     * @param Options
     */
    private void initOptionsPickerView(List<String> Options, final Context context){
        OptionsPickerView pickerView = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        RoomChooseFragment roomChooseFragment = new RoomChooseFragment();
                        //设置fragmnet向activity通信
                        roomChooseFragment.setDataBcakListener(new RoomChooseFragment.DataBcakListener() {
                            @Override
                            public void DataBcakListener(String data) {
                                Message mess = new Message();
                                mess.obj = data;
                                mess.what = DIALOGFRAGMNET_STUATE;
                                mHandler.handleMessage(mess);
                            }
                        });
                        roomChooseFragment.setmContext(context);
                        //activity向fragment传递数据
                        Bundle bundle = new Bundle();
                        bundle.putString("buliding","");
                        bundle.putString("floor","");
                        bundle.putString("room","");
                        roomChooseFragment.setArguments(bundle);
                        //fragmnet展开
                        roomChooseFragment.show(getSupportFragmentManager(),"");
                    }
                },300);
            }
        }).build();
        pickerView.setPicker(Options);
        pickerView.show();
    }

    /**
     * 弱引用handler
     */
    private class MHandler extends Handler {
        private WeakReference<Activity> reference;
        public MHandler(Activity activity) {
            // 使用WeakReference弱引用持有Activity实例
            reference = new WeakReference<Activity>(activity);
        }
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DIALOGFRAGMNET_STUATE:
                    break;
                case BATTERY_STUATE:
                    break;
                default:
            }
        }
    }

    /**
     * 电池状态广播
     */
    private class MyRecvice extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            int level = 0;
            Intent batteryInfoIntent = intent;
            level = batteryInfoIntent.getIntExtra("level",0);
            int batterySum = batteryInfoIntent.getIntExtra("scale", 100);
            int percentBattery= 100 *  level / batterySum;
            Log.i("Main","level = " + level);
            Log.i("Main","batterySum = " + batterySum);
            Log.i("Main","percent is " +  percentBattery+ "%");
            if (mHandler!=null) {
                Message mes = new Message();
                mes.what = BATTERY_STUATE;
                mes.obj = percentBattery;
                mHandler.handleMessage(mes);
            }
        }
    }

    /**加载测试数据
     * @param objectBoxHelper
     */
    private void initTest(ObjectBoxHelper objectBoxHelper){
//        int num=0;
//        String name = "";
//        DepartmentList departmentList = new DepartmentList();
//        objectBoxHelper.removeall();
//        List<DeviceEntity>entities =new ArrayList<>();
//        List<String>beanList = new ArrayList<>();
//        PCInfoBean infoBean = new Gson().fromJson("{\"code\":200,\"data\":{\"check\":[{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号4\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"2222222\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号31\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"70490\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号29\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"puqjfr\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号24\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号13\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号5\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号26\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"puqjfr\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103120191564398740521\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号1\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号21\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号18\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号8\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号17\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号20\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号6\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号19\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"puqjfr\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号14\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号27\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"puqjfr\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"puqjfr\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号25\"},{\"deviceStreamCode\":\"77771103120191564398740521\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号3\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号16\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号28\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号22\"},{\"deviceStreamCode\":\"77771103120191564398740521\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号2\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号10\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号23\"},{\"deviceStreamCode\":\"77771103420191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"绵阳处级仓库\",\"locationPosition\":\"2\",\"locationFloor\":\"3\",\"locationRoom\":\"d977502490b74e8f813b6386c7614505\",\"locationIbm\":\"装备位置--机构号30\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"puqjfr\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号7\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"puqjfr\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号9\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号12\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号11\"},{\"deviceStreamCode\":\"77771103null20191564398740522\",\"deviceRealName\":\"烨伟\",\"useDepartment\":\"puqjfr\",\"locationPosition\":\"u0flhj\",\"locationFloor\":\"qz9q88\",\"locationRoom\":\"h5ejbd\",\"locationIbm\":\"5xrv8s\"},{\"deviceStreamCode\":\"77771103120191564398740522\",\"deviceRealName\":\"大白菜\",\"useDepartment\":\"局级\",\"locationPosition\":\"1\",\"locationFloor\":\"033e7bab1a3f420da9abce7db4ff4e9d\",\"locationRoom\":\"dfafc2b8e94e4adebfe92c92290f81f2\",\"locationIbm\":\"装备位置--机构号15\"}]},\"message\":\"操作成功\"}", PCInfoBean.class);
//        for (int i=0;i<infoBean.getData().getCheck().size();i++){
//            PCInfoBean.DataBean.CheckBean checkBean = infoBean.getData().getCheck().get(i);
//            DeviceEntity deviceEntity = new DeviceEntity(checkBean.getDeviceStreamCode(),checkBean.getUseDepartment(),checkBean.getLocationPosition(),
//                    checkBean.getLocationFloor(),checkBean.getLocationRoom(),checkBean.getLocationIbm(),checkBean.getDeviceRealName(),false);
//            entities.add(deviceEntity);
//            if (i==0){
//                name = checkBean.getUseDepartment();
//                beanList.add(checkBean.getUseDepartment());
//            }else {
//                    if (!name.equals(checkBean.getUseDepartment())){
//                        int o=0;
//                        for (int j=0;j< beanList.size();j++) {
//                            if (!beanList.get(j).equals(checkBean.getUseDepartment())) {
//                                o++;
//                            }
//                        }
//                        if (o==beanList.size()){
//                            beanList.add(checkBean.getUseDepartment());
//                        }
//                    }
//            }
//        }
//        departmentList.setDepartmentList(beanList);
//        departmentList.setNum(infoBean.getData().getCheck().size());
//        DepartmentFileutil.SaveDepartment(departmentList);
//        objectBoxHelper.addlist(entities);
        List<DeviceEntity>deviceEntities = new ArrayList<>();
        for (int i=0;i<5;i++) {
            DeviceEntity deviceEntity = new DeviceEntity();
            deviceEntity.setUsername("qrcode");
            deviceEntity.setDeviceStreamCode(String.valueOf(1210+i));
            deviceEntities.add(deviceEntity);
        }
        objectBoxHelper.RxAddList(deviceEntities);
    }


    /**
     * 初始化列表数据
     */
    private void initrecyclerViewdata(DepartmentList departmentList){
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        useDepartmentAdpter =new UseDepartmentAdpter(departmentList,this);
        recyclerView.setAdapter(useDepartmentAdpter);
    }

    /**
     *初始化UI文本
     */
     private void initdata(){
         if (ObjectBoxHelper.getInstance().Query("user", DeviceEntity_.username).size()>0) {
             DepartmentFileutil.GetDepartment().subscribe(new Consumer<DepartmentList>() {
                 @Override
                 public void accept(DepartmentList o) throws Exception {
                     if (useDepartmentAdpter == null) {
                         initrecyclerViewdata(o);
                     } else {
                         useDepartmentAdpter.setDepartmentList(o);
                         useDepartmentAdpter.notifyDataSetChanged();
                     }
                     num_partment.setText("" + o.getDepartmentList().size());
                     left_partment.setText("" + o.getDepartmentList().size());
                     all_device.setText("" + o.getNum());
                 }
             });
         }
     }

    /**弹出选择框
     * @param v
     */
    private void popwindowshow(View v){
        View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popwindow_layout,null);
        PopupWindow mPopWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);;
        mPopWindow.setContentView(contentView);
        mPopWindow.setFocusable(true);
        contentView.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SweetAlertDialog(MainActivity.this, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("是否要退出盘点")
                        .setContentText("退出盘点后系统将自进行本地数据库更新")
                        .setCancelText("否")
                        .setConfirmText("确定")
                        .AddLoading(new AVLoadingIndicatorView(getApplicationContext()))
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(final SweetAlertDialog sDialog) {
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        sDialog
                                                .setTitleText("完成!")
                                                .setContentText("盘点数据已经更新到本地数据库!")
                                                .setCancelText(null)
                                                .setConfirmText((null))
                                                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                                    @Override
                                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                                        SharedPrefsUtil.putValue(MainActivity.this,"logon","");
                                                        Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
                                                        intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
                                                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                        MainActivity.this.startActivity(intent);
                                                        MainActivity.this.finish();
                                                    }
                                                })
                                                .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                                        MainActivity.this.finish();
                                    }
                                },1000);
                            }
                        })
                        .show();
                mPopWindow.dismiss();
            }
        });
        //跳转到日志
        contentView.findViewById(R.id.ces).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopWindow.dismiss();
                startActivity(new Intent(MainActivity.this, LogActivity.class));
                overridePendingTransition(R.anim.bottom_in,R.anim.buttom_out);
            }
        });
        //跳转到扫码界面
        contentView.findViewById(R.id.out_room).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    mPopWindow.dismiss();
                    Intent i  =new Intent(MainActivity.this, StartActivity.class);
                    Bundle bundle  =new Bundle();
                    bundle.putStringArray("status",new String[]{"扫码出库"});
                    i.putExtras(bundle);
                    startActivity(i);
                    overridePendingTransition(R.anim.bottom_in,R.anim.buttom_out);
            }
        });
        mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
             backgroundAlpha(1.0f);
            }
        });
        //显示PopupWindow
        mPopWindow.showAsDropDown(v,20,0);
        backgroundAlpha(0.6f);
    }

    // 设置屏幕透明度
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0~1.0
        this.getWindow().setAttributes(lp); //act 是上下文context
    }
}